<?php

/**
 * Project: Espolon
 * Licence: BSD License
 * Link: http://espolon.org/
 * Copyright: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Author: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Version: Beta
 */

if (!defined('SITE_ROOT')) exit('No direct script access allowed');

/**
 * Check perms and module status on/off
 */

$this->checkAccess($module, in_array($command, $db->GetCol("SELECT name FROM mod_sections WHERE level=0")) ? 'view' : $command);
$tpl->assign('active', $module);
$tpl->assign('menu_active', $command);

function __autoload($class_name) {
  require_once 'class.'.strtolower($class_name).'.php';
}

$section = new Section();

foreach ($section->getSections() as $value) {
  $sec_name[] = $value['name'];
}

$sec_name = empty($sec_name) ? array() : $sec_name;

$tpl->assign('submenu',$sec_name);

switch ($command) {
  case 'delete':
    $command_tpl = 'delete.tpl';
    $section->setId($options[0]);
    if($section->isLeaf()) {
      $tpl->assign('section',$section->getSections($options[0]));
      if (isset($options[1]) AND $options[1] == 'true') {
        $section->setId($options[0]);
        $tpl->assign('result', $section->delete());
      }
    } else {
      $tpl->assign('noleaf', true);
    }
  break;
  default:
    if ($command == 'modify') {
      $tpl->assign('modify', true);
      $section->setId($options[0]);
    } else {
      $tpl->assign('sections', $section->display_tree());
    }

    if (in_array($command, $sec_name) || isset($options[0])) {
      $section->getTree((isset($options[0])) ? $options[0] : $command);
      if ($command != 'modify') {
        $tpl->assign('sections', $section->display_tree($section->getRoot_id()));
        $tpl->assign('sections_path', implode('/', $section->getPath()));
        $tpl->assign('menu_active', array_shift($section->getPath()));
      }
    }

    if (empty($command)) {
      $command_tpl = 'addsection.tpl';
      $form = new AddSection($tpl, $section);
    } else {
      $command_tpl = 'addnode.tpl';
      $form = new AddNode($tpl, $section);
    }
    if ($form->validate()) {
      $form->freeze();
      $form->process();
    } else {
      $form->displayForm();
    }
  break;
}

/**
 * display module
 */
$tpl->assign('command_tpl',"$template/modules/$module/".((isset($command_tpl)) ? $command_tpl : "$command.tpl"));
$tpl->assign('module_tpl', "modules/$module/$module_tpl");
$tpl->display($template.'/base.tpl');

?>